//
//  KnotViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/14.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "KnotViewController.h"
#import "ResizeView.h"
#import "HttpOrgAction.h"

@interface KnotViewController (){
    KnotBlock knotBlock;
}
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UITextField *markTF;
@property (nonatomic,strong) NSString *orgId;
@end

@implementation KnotViewController

-(id)initWithOrgId:(NSString*)orgId  block:(KnotBlock)block{
    if (self == [super init]) {
        self.orgId = orgId;
        knotBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"结款";
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    ResizeViewInfo *moneyRV = [self getNormalResizeViewInfo:@"金额:"];
    self.nameTF = (UITextField*)[moneyRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:moneyRV];
    
    ResizeViewInfo *markRV = [self getNormalResizeViewInfo:@"备注:"];
    self.markTF = (UITextField*)[markRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:markRV];
    
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    InCashActionInfo *inCashActionInfo = [[InCashActionInfo alloc] init];
    inCashActionInfo.orgId = self.orgId;
    inCashActionInfo.amount = [self.nameTF.text floatValue];
    inCashActionInfo.comment = self.markTF.text;
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpOrgAction getOrgIntoCash:inCashActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            if (knotBlock) {
                knotBlock();
            }
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];

            } afterDelay:0.8];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
